package com.mspbots.ticketai.graderule;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mspbots.ticketai.GradeRuleInterceptorAdapter;
import com.mspbots.ticketai.model.Ticket;
import com.mspbots.ticketai.model.TicketRule;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author Harry hou
 * @date 2019/11/15 15:43
 */
@Service(value = "AGREEMENT_POINT_RULE")
public class AgreementRuleInterceptor extends GradeRuleInterceptorAdapter {

    private static final String AGREEMENT_MRR = "agreementMrr";
    private List<AgreementRulesParams> params;

    /**
     * 1.prepare data: get agreement value from tickets
     * 2.prepare rule data: get agreement rule from front end
     * 3.calculate the agreement point result
     */
    @Override
    public void prepare(TicketRule rule) {
        this.params = this.objectMapper.convertValue(rule.getParams(), new TypeReference<>() {
        });
    }

    @Override
    public void process(Ticket ticket) {
        int order = 0;
        Double billAmount = ticket.getBillAmount();
        if (!ObjectUtils.isEmpty(billAmount)) {
            for (AgreementRulesParams item : params) {
                if (!ObjectUtils.isEmpty(item.getMinAmount()) || !ObjectUtils.isEmpty(item.getMaxAmount())) {
                    if (ObjectUtils.isEmpty(item.getMinAmount())) {
                        if (billAmount <= item.getMaxAmount()) {
                            order = item.getPoints();
                            break;
                        }
                    } else if (ObjectUtils.isEmpty(item.getMaxAmount())) {
                        if (billAmount >= item.getMinAmount()) {
                            order = item.getPoints();
                            break;
                        }
                    } else {
                        if (billAmount <= item.getMaxAmount() && billAmount >= item.getMinAmount()) {
                            order = item.getPoints();
                            break;
                        }
                    }
                }
            }
        }
        ObjectNode jsonNode = ticket.getOrderDetail() == null ? objectMapper.createObjectNode() : ticket.getOrderDetail().deepCopy();
        if (ticket.getIsShowMRR()) {
            ObjectNode jsonNodeParams = objectMapper.createObjectNode();
            jsonNodeParams.put("agreementRuleParams", billAmount);
            jsonNodeParams.put("point", order);
            jsonNode.putPOJO(AGREEMENT_MRR, jsonNodeParams);
        } else {
            jsonNode.putPOJO(AGREEMENT_MRR, null);
        }
        ticket.setOrderDetail(jsonNode);
        ticket.setOrder(ticket.getOrder() + order);
    }

    @Data
    private static class AgreementRulesParams {
        private Double minAmount;
        private Double maxAmount;
        private Integer points;
        private boolean selected;
    }
}
